<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :title="isEdit ? '修改项目定义' : '新增项目定义'"
    width="800px"
  >
    <el-form :model="form" ref="form" label-width="100px" :rules="rules">
      <el-form-item label="上级分类">
        <el-select clearable style="width: 100%" v-model="form.internalTypeId" placeholder="请选择">
          <el-option
            v-for="item in selectData"
            :key="item.internalTypeId"
            :value="item.internalTypeId"
            :label="item.internalTypeName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="internalItemName">
        <el-input v-model="form.internalItemName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="项目类型" prop="internalItemType">
        <el-select clearable style="width: 100%" v-model="form.internalItemType" placeholder="请选择">
          <el-option
            v-for="item in list"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="itemOrder">
        <el-input type="number" v-model.number="form.itemOrder" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { defineCreate, defineUpdate, typeAll } from '@/api/satisfaction'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      list: [
        {
          value: 1,
          label: '科间互评'
        },
        {
          value: 2,
          label: '科内互评'
        },
        {
          value: 3,
          label: '评上级领导'
        },
        {
          value: 4,
          label: '评院领导'
        }
      ],
      visible: false,
      isEdit: false,
      form: {
        internalTypeId: null,
        internalItemName: null,
        internalItemType: null,
        itemOrder: 0
      },
      selectData: [],
      rules: {
        internalItemName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        internalItemType: [{ required: true, message: '请选择项目类型', trigger: 'blur' }],
        leaf: [{ required: true, message: '请选择是否末级', trigger: 'blur' }],
        itemOrder: [{ type: 'number', message: '请输入数字', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    typeAll().then(res => {
      console.log('typeAll res', res)
      this.selectData = res.data
    })
  },
  methods: {
    edit(bool, obj) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, obj)
      if (!bool) {
        this.$set(this.form, 'itemOrder', 0)
        this.$set(this.form, 'leaf', 0)
      }
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await defineUpdate(this.form)
          } else {
            mes = '新增操作成功'
            res = await defineCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
